package com.yuan.mint.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.yuan.mint.R;
import com.yuan.mint.activity.RecommendDetailActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RecommendFragment extends Fragment implements AdapterView.OnItemClickListener {

    private static final int RECOMMEND_DIET = 0;
    private static final int RECOMMEND_EXERCISE = 1;
    private static final int RECOMMEND_MUSIC = 2;
    private static final int RECOMMEND_WEATHER = 3;
    private static final int RECOMMEND_HEALTH = 4;

    public static String DIET_NAME = "diet";
    public static String EXERCISE_NAME = "exercise";
    public static String MUSIC_NAME = "music";
    public static String WEATHER_NAME = "weather";
    public static String HEALTH_NAME = "health";


    private ListView recommendList;
    private SimpleAdapter reAdapter;
    List<Map<String, Object>> listData = new ArrayList<Map<String, Object>>();
    protected String reStr[] = null;
    protected int images[] = null;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_recommend, container, false);
        recommendList = (ListView) view.findViewById(R.id.recommend_list);
        setListData();
        initView(view);
        return view;
    }

    protected void setListData() {
        reStr = new String[]{"健康饮食", "运动分享", "音乐分享", "天气", "健康"};
        images = new int[]{R.mipmap.ic_launcher, R.mipmap.ic_launcher,
                R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};
    }

    private void initView(View view) {
        getItemData();
        reAdapter = new SimpleAdapter(view.getContext(), listData,
                R.layout.recommend_list_item, new String[]{"img", "text"}, new int[]{R.id.img, R.id.text});
        recommendList.setAdapter(reAdapter);
        recommendList.setOnItemClickListener(this);
    }

    //用来实例化列表容器的函数
    private void getItemData() {
        for (int i = 0; i < reStr.length; i++) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("img", images[i]);
            map.put("text", reStr[i]);
            listData.add(map);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case RECOMMEND_DIET:
                startActivity(new Intent(getActivity(), RecommendDetailActivity.class).putExtra("name", DIET_NAME));
                break;
            case RECOMMEND_EXERCISE:
                startActivity(new Intent(getActivity(), RecommendDetailActivity.class).putExtra("name", EXERCISE_NAME));
                break;
            case RECOMMEND_MUSIC:
                break;
            case RECOMMEND_WEATHER:
                break;
            case RECOMMEND_HEALTH:
                break;
        }
    }
}
